package org.qydjk.console.service.impl;

import org.qydjk.common.constant.RedisConstant;
import org.qydjk.common.dao.IBaseDao;
import org.qydjk.common.pojo.SellerProductLabel;
import org.qydjk.common.pojo.SellerProductLabelDetail;
import org.qydjk.common.service.impl.BaseService;
import org.qydjk.common.util.BeanUtil;
import org.qydjk.common.util.RedisTemplateUtil;
import org.qydjk.common.util.StringUtil;
import org.qydjk.common.vo.EasyUIResponse;
import org.qydjk.common.vo.HibernatePage;
import org.qydjk.common.vo.redisvo.RedisSellerProductLabel;
import org.qydjk.common.vo.redisvo.RedisSellerProductLabelDetail;
import org.qydjk.console.service.ISellerProductLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * 标签的业务实现
 * Created by Edward_yang on 2017/12/12 0012.
 */
@Service
public class SellerProductLabelService extends BaseService  implements ISellerProductLabelService{
        @Autowired
        private IBaseDao baseDao;

    /****
     * 带分页查询和筛选标签
     * @author Edward_yang
     * @uptime 2017-12-12
     * @param page 当前页
     * @param rows 每页多少条
     * @param one_category_id  联动
     * @param keyWord  模糊查询
     * @param type     下拉分类
     * @return EasyUIResponse  total和List
     */
    public EasyUIResponse findLabelList(Integer page, Integer rows, String one_category_id, String keyWord, String type) throws  Exception{
        HibernatePage pager = new HibernatePage(page,rows);
        String sql=" select * from qy_seller_product_label qspl  where 1=1 ";
        String hqlCount="select count(spl.id) from SellerProductLabel spl where 1=1 ";
        //联动处理
        if(!StringUtil.isEmptyOrNull(one_category_id)){
            sql+="  and qspl.one_category_id="+one_category_id;
            hqlCount+="  and spl.oneCategoryId='"+one_category_id+"'";
        }
        //查询
        if(!StringUtil.isEmptyOrNull(keyWord)){
            sql+="  and qspl.name like '%"+keyWord+"%' or  qspl.code like '%"+keyWord+"%'";
            hqlCount+=" and spl.name like '%"+keyWord+"%' or  spl.code like '%"+keyWord+"%'";
        }
        //下拉筛选
        if(!StringUtil.isEmptyOrNull(type)){
            sql+=" and qspl.type="+type;
            hqlCount+=" and spl.type="+type;
        }
        sql+= " order by qspl.ord desc ";
            List<SellerProductLabel> sellerProductLabelList =baseDao.findBySQL(sql, SellerProductLabel.class,pager);
            int total = baseDao.getRecordCountHQL(hqlCount);
            return new EasyUIResponse(total,sellerProductLabelList);

    }
    /***
     *  查询标签所有的数据
     * @author Edward_yang
     * @uptime 2017-12-12
     *  @return  List<SellerProductLabel>
     */
    public List<SellerProductLabel> findAllList() throws Exception {
        String sql=" select * from qy_seller_product_label qspl order by qspl.ord desc ";
        List<SellerProductLabel>  sellerProductLabelList =baseDao.findBySQL(sql, SellerProductLabel.class);
        return sellerProductLabelList;
    }

    public void getLabelsToRedis(RedisTemplateUtil redisTemplateUtil) throws Exception {
        //查询所有商家、产品标签
        List<SellerProductLabel> spls = baseDao.findByHql("from SellerProductLabel");
        //查询所有商家、产品标签明细
        List<SellerProductLabelDetail> splds = baseDao.findByHql("from SellerProductLabelDetail");

        //全部商家标签
        List<RedisSellerProductLabel> sLabelList = new ArrayList<RedisSellerProductLabel>();
        //全部产品标签
        List<RedisSellerProductLabel> pLabelList = new ArrayList<RedisSellerProductLabel>();

        for (SellerProductLabel label : spls){
            List<RedisSellerProductLabelDetail> detailList = new ArrayList<RedisSellerProductLabelDetail>();
            for (SellerProductLabelDetail labelDetail : splds){
                if(label.getCode().equals(labelDetail.getSellerProductLabelCode())){
                    RedisSellerProductLabelDetail detail = new RedisSellerProductLabelDetail();
                    BeanUtil.copyProperties(labelDetail,detail);
                    detailList.add(detail);
                }
            }

            if(detailList.size() > 0){
                RedisSellerProductLabel l = new RedisSellerProductLabel();
                BeanUtil.copyProperties(label,l);
                l.setDetails(detailList);
                if(label.getType().intValue() == 0){
                    sLabelList.add(l);
                }else{
                    pLabelList.add(l);
                }
            }

        }

        //放入缓存
        redisTemplateUtil.setWithByte(RedisConstant.SELLER_LABEL_KEY,sLabelList);
        redisTemplateUtil.setWithByte(RedisConstant.PRODUCT_LABEL_KEY,pLabelList);
    }

    private void buildLabel(SellerProductLabel sspl,List<RedisSellerProductLabel> sList,List<RedisSellerProductLabelDetail> ssList){
        RedisSellerProductLabel label = new RedisSellerProductLabel();
        BeanUtil.copyProperties(sspl,label);
        label.setDetails(ssList);
        sList.add(label);
    }
}
